package top.txl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.txl.entity.Category;
import top.txl.entity.Flavors;
import top.txl.entity.PageBean;
import top.txl.exception.CustomerException;
import top.txl.mapper.CategoryMapper;
import top.txl.service.CategoryService;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void add(Category category) {
        categoryMapper.add(category);
    }

    @Override
    public void edit(Category category) {
        categoryMapper.edit(category);
    }

    @Override
    public void delete(Integer id) {
        // 判断当前删除的分类是否绑定菜品
        Integer count1 = categoryMapper.countD(id);
        if(count1 > 0){
            // 抛出异常
            throw new CustomerException("此分类已绑定菜品，请解绑后再进行删除");
        }
        // 判断当前删除的分类是否绑定套餐
        Integer count2 = categoryMapper.countM(id);
        if(count2 > 0){
            throw new CustomerException("此分类已绑定套餐，请解绑后再进行删除");
        }
        categoryMapper.delete(id);
    }

    @Override
    public PageBean lists(Integer page, Integer pageSize,Integer type) {
        PageHelper.startPage(page,pageSize);
        List<Category> lists = categoryMapper.lists(type);
        Page p = (Page<Category>) lists;
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return pageBean;
    }

    @Override
    public Category detail(Integer id) {
        Category category = categoryMapper.detail(id);
        return category;
    }

    /**
     * 口味列表
     * @return
     */
    @Override
    public List<Flavors> flavors() {
       List<Flavors> lists = categoryMapper.flavors();
       return lists;
    }


}
